***************************************************************************************
*This file call on other data processing files, merge data, and create trend variables
***************************************************************************************

* Change the following directory to where the replication folder is stored
global root `c(pwd)'

clear all

/* 
The replication package does not include the raw weather data provided by Wolfram Schlenker. The data can be downloaded from https://wolfram-schlenker.info/links.html
Once the data are downloaded, users can run fips_state_append.do and weather_raw.do to generate weather_month.dta used in this study. This replication package has included weather_month.dta to ease the replication process.
*/

do 	"$root/dofiles/data/ag_data.do"
do  "$root/dofiles/data/alt_drought_data.do"
do 	"$root/dofiles/data/insurance_data.do"
do  "$root/dofiles/data/usdm_data.do"
do  "$root/dofiles/data/weather_altdrought_data.do"

use "$root/data/temp/ag_data.dta", replace
ren stateansi st_fips
drop if missing(fips)
merge 1:1 fips year using data\temp\c_usdm.dta, nogen
merge 1:1 fips year using data\temp\s_usdm.dta, nogen
merge 1:1 fips year using data\temp\c_weather_altdrought.dta, nogen 
merge 1:1 fips year using data\temp\s_weather_altdrought.dta, nogen 
merge 1:1 fips year using data\temp\insurance.dta, nogen
merge m:1 fips using data\raw\county_lat_long.dta, nogen

drop if year <= 1999 | year >= 2024 

**# Generate addditional variables for final use
xtset fips year
gen yr = year
replace year = year - 1999
gen year2 = year^2
gen stateyear = st_fips*100 + year
gen state = st_fips

foreach crop in corn soybeans {
	replace `crop'_harvested = `crop'_production/`crop'_yield if missing(`crop'_harvested)
	replace `crop'_harvested = 0 if `crop'_yield == 0 & missing(`crop'_harvested)
	replace `crop'_production = 0 if `crop'_yield == 0 & missing(`crop'_production)
	gen `crop'_hratio = `crop'_harvested/`crop'_planted 
	gen `crop'_pplanted = `crop'_planted/`crop'_plratio

	gen ln`crop'_pplanted = ln(`crop'_pplanted)
	gen ln`crop'_planted = ln(`crop'_planted)
	gen ln`crop'_harvested = ln(`crop'_harvested)
	gen ln`crop'_yield = ln(`crop'_yield)
	gen ln`crop'_production = ln(`crop'_production)
	gen ln`crop'_hratio = ln(`crop'_hratio)
	gen ln`crop'_plratio = ln(`crop'_plratio)
	gen ln`crop'_planned = ln(`crop'_planted_fsa + `crop'_prevented_fsa + `crop'_failed_fsa)
	gen `crop'_restrict=0
	replace `crop'_restrict=1 if ~(missing(ln`crop'_pplanted) | missing(ln`crop'_plratio) | missing(ln`crop'_hratio) | missing(ln`crop'_yield) | missing(ln`crop'_production))
	egen N_year_`crop' = count(ln`crop'_harvested), by (fips)
}

**# Label drought variables
foreach c in 0 1 2 3 4 {
	foreach s in b p g {
		label variable s_d`c'_`s' "D`c'"
		label variable c_d`c'_`s' "D`c'"
	}
}

**# Calculate the % of time in drought per season
foreach crop in c s {
	foreach ss in b p g {
		gen `crop'_prec_`ss'2 = `crop'_prec_`ss'^2
			
		egen `crop'_tot_`ss' = rowtotal(`crop'_d0_`ss' `crop'_d1_`ss' `crop'_d2_`ss' `crop'_d3_`ss' `crop'_d4_`ss')
		
		forvalues d = 0/4 {
			gen `crop'_d`d'_`ss'_w = `crop'_d`d'_`ss'
			replace `crop'_d`d'_`ss' = `crop'_d`d'_`ss'*7/`crop'_sl_`ss'
		}

		replace `crop'_tot_`ss' = `crop'_tot_`ss'*7/`crop'_sl_`ss'

		gen `crop'_d3_`ss'_o = `crop'_d3_`ss' 
		gen `crop'_d3_`ss'_o_w = `crop'_d3_`ss'_w 
		replace `crop'_d3_`ss' = `crop'_d3_`ss' + `crop'_d4_`ss' 
		replace `crop'_d3_`ss'_w = `crop'_d3_`ss'_w + `crop'_d4_`ss'_w 	
	}
}

drop *d4* *d5*

save "$root\data\use.dta", replace